package Dao.impl;

import Dao.BaseDao;
import Dao.SongsDao;
import pojo.Songs;

import java.util.List;

/**
 * 作者：杨晋
 */
public class SongsDaoImpl extends BaseDao<Songs> implements SongsDao {

    public boolean insert(Songs songs) {
        // 下面给我写一个mySql语句，用来插入，主要包歌单括title,filePath,description,uploaderId 五个字段
        String sql = "insert into songs(title,filePath,description,uploaderId,categoryId) values(?,?,?,?,?)";
        // 添加数据是用update
        int result = update(sql, songs.getTitle(), songs.getFilePath(), songs.getDescription(), songs.getUploaderId(),songs.getCategoryId());
        return result > 0;
    }

    @Override
    public boolean update(Songs songs) {
     String sql = "UPDATE songs SET title =?, filePath =?, description =?, uploaderId =?, categoryId =? WHERE songId =?";
        int result = update(sql, songs.getTitle(), songs.getFilePath(), songs.getDescription(), songs.getUploaderId(), songs.getCategoryId(), songs.getSongId());
        return result > 0;
    }

    @Override
    public List<Songs> getSongsByUploaderId(int uploaderId) {
        // 根据 uploaderId 查询对应的歌曲
        String sql = "SELECT songId, title, filePath, description, uploaderId, categoryId FROM songs WHERE uploaderId = ?";
        // 调用 BaseDao 的 getBeanList 方法，获取查询结果
        return getBeanList(sql, uploaderId);
    }

    @Override
    public boolean deleteById(int songId) {
        String sql = "DELETE FROM songs WHERE songId =?";
        int result = update(sql, songId);
        return result > 0;
    }

    @Override
    public List<Songs> selectSongList(Integer startIndex, Integer targetTotal) {
        String sql = "select * from songs limit ?, ?";
        List<Songs> list = getBeanList(sql, startIndex, targetTotal);
        return list;
    }

    @Override
    public List<Songs> selectSongList() {
        return getBeanList("select * from songs");
    }

    @Override
    public String getSongPathById(int songId) {
        String sql = "select filePath from songs where songId = ?";
        return getBean(sql, songId).getFilePath();
    }

    @Override
    public Songs selectSongById(Integer songId) {
        String sql = "select * from songs where songId = ?";
        return getBean(sql, songId);
    }
}
